package com.example.qxapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qxapp.Bean.Post;
import com.example.qxapp.R;
import com.example.qxapp.activity.ReciveActivity;
import com.example.qxapp.activity.dlActivity;

import java.util.List;
import java.util.logging.LogRecord;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.http.I;

public class syadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Post> data;
    private final int n_type=0;
    private final int f_type=1;
    private int maxnum=7;
    private Boolean isfootview=true;


    public syadapter(Context context, List<Post> data){
        this.context=context;
        this.data=data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.ord_item,parent,false);
        View footview=LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_item,parent,false);
        if (viewType==f_type){
            return new RecyclerViewHolder(footview,f_type);
        }else {
            return new RecyclerViewHolder(view,n_type);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        if (isfootview&&(getItemViewType(i))==f_type){
            //底部加载
            final RecyclerViewHolder recyclerViewHolder= (RecyclerViewHolder) holder;
            recyclerViewHolder.loading.setText("加载中....");
            Handler handler=new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    maxnum+=8;
                    notifyDataSetChanged();
                }
            },2000);
        }else {
            //orditem内容
            final RecyclerViewHolder recyclerViewHolder= (RecyclerViewHolder) holder;
            Post post=data.get(i);
            recyclerViewHolder.username.setText(post.getName());
            recyclerViewHolder.nickname.setText(post.getNickname());
            recyclerViewHolder.content.setText(post.getContent());
            recyclerViewHolder.time.setText(post.getCreatedAt());
            recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=recyclerViewHolder.getAdapterPosition();
                    if (BmobUser.getCurrentUser(BmobUser.class)!=null){
                        Intent intent=new Intent(context, ReciveActivity.class);
                        intent.putExtra("username",post.getName());
                        intent.putExtra("content",post.getContent());
                        intent.putExtra("time",post.getCreatedAt());
                        intent.putExtra("id",data.get(position).getObjectId());
                        context.startActivity(intent);
                    }else {
                        Toast.makeText(context,"请登录",Toast.LENGTH_SHORT).show();
                        context.startActivity(new Intent(context, dlActivity.class));
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position==maxnum-1){
            return f_type;
        }else {
            return n_type;
        }
    }

    @Override
    public int getItemCount() {
        if (data.size()<maxnum){
            return data.size();
        }
        return maxnum;
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView nickname,content,time,username;//orditem的textview
        public TextView loading;
        public RecyclerViewHolder(View itemview, int view_type) {
            super(itemview);
            if (view_type==n_type){
                username=itemview.findViewById(R.id.username);
                nickname=itemview.findViewById(R.id.nickname);
                content=itemview.findViewById(R.id.content);
                time=itemview.findViewById(R.id.time);
            }else if (view_type==f_type){
                loading=itemview.findViewById(R.id.foottext);
            }
        }
    }
}
